/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework Class Library
 * Version 2.00
 * 
 * Asymmetric Cryptographic Device Base Class
 *      Provides a base class definition for cryptographic implementations that
 * use asymmetric algorithms with a key value.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;

namespace Adaptive.Foundation.Security
{
    /// <summary>
    /// Provides a base class definition for cryptographic implementations that
    /// use asymmetric algorithms with a key value.
    /// </summary>
    public abstract class AsymmetricCryptographicDeviceBase : CryptographicDeviceBase
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Key value.
        /// </summary>
        private byte[] _key;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public AsymmetricCryptographicDeviceBase()
            : base()
        {
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if ((!Disposed) && (disposing))
            {
                Clear();
            }
            _key = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets or sets the value of the encryption/descryption key.
        /// </summary>
        /// <value>
        /// A symmetric algorithm encryption or decryption key value.
        /// </value>
        public byte[] Key
        {
            get { return _key; }
            set
            {
                if (ValidateKey(value))
                    _key = value;
            }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Validates the specified content as a symmetric encryption key.
        /// </summary>
        /// <param name="keyData">
        /// The data being assigned as the encryption or decryption key.
        /// </param>
        /// <remarks>
        /// Each algorithm's implementation may use keys of varying sizes; thus the 
        /// child implementation must validate the key value accordingly.
        /// </remarks>
        /// <returns>
        /// <b>true</b> if the data is valid; otherwise, <b>false</b>.
        /// </returns>
        protected abstract bool ValidateKey(byte[] keyData);
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Clears the contents of the object instance without disposing of the instance.
        /// </summary>
        public override void Clear()
        {
            if (_key != null)
                Array.Clear(_key, 0, _key.Length);
        }
        #endregion
    }
}